package com.yycx.module.bus.provider.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.util.TypeUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.security.OpenUser;
import com.yycx.common.utils.ApiAssert;
import com.yycx.common.utils.ConvertUtils;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.bus.client.entity.*;
import com.yycx.module.bus.client.entity.hy.BusPolicyDrug;
import com.yycx.module.bus.client.enums.DrugTypeEnum;
import com.yycx.module.bus.provider.mapper.BusDrugsMapper;
import com.yycx.module.bus.provider.service.BusDrugsService;
import com.yycx.module.bus.provider.service.hy.BusPolicyDrugService;
import com.yycx.module.bus.provider.service.hy.HyOrderDetailService;
import com.yycx.module.bus.provider.service.hy.HyRemoteOrderService;
import com.yycx.module.system.client.entity.SysCompany;
import com.yycx.module.system.provider.service.SysCompanyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.*;

/**
 * 药品接口实现类
 *
 * @author flyme
 * @date 2020-07-26
 */
@Service
@Slf4j
public class BusDrugsServiceImpl extends BaseServiceImpl<BusDrugsMapper, BusDrugs> implements BusDrugsService {
    @Autowired
    private SysCompanyService companyService;

    @Autowired
    private HyRemoteOrderService hyRemoteOrderService;

    @Autowired
    private BusPolicyDrugService policyDrugService;

    @Autowired
    @Lazy
    private HyOrderDetailService orderDetailService;

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusDrugs drugs, EntityMap extra) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        Long organizationId = company.getOrganizationId();
        drugs.setCompanyId(companyId);
        drugs.setOrganizationId(organizationId);
        drugs.setDrugsState(1);
        return ResultBody.ok();
    }


    @Override
    public ResultBody beforeListEntityMap(CriteriaQuery<BusDrugs> cq, BusDrugs busDrugs, EntityMap requestMap) {
        cq.eq("supplierId", requestMap.getLong("supplierId"));
        return super.beforeListEntityMap(cq, busDrugs, requestMap);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusDrugs> cq, BusDrugs drugs, EntityMap requestMap) {
        String drugsName = drugs.getDrugsName();
        cq.select(BusDrugs.class, "*");
        if (FlymeUtils.isNotEmpty(drugsName)) {
            cq.and(e -> e.likeRight("drugs.drugsName", drugsName).or().likeRight("drugs.drugCode", drugsName.toUpperCase()));
        }
        cq.eq(BusDrugs.class, "supplierId");
        cq.eq(BusDrugs.class, "drugType");
        cq.eq(BusDrugs.class, "organizationId");
        cq.eq(BusDrugs.class, "ownerState");
        cq.orderByDesc("drugs.createTime");
        return ResultBody.ok();
    }


    @Override
    public BusDrugs selectByDrugNameAndSpec(String drugsName, String drugsSpec, String producersName) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugs.class);
        cq.eq("drugsName", drugsName);
        cq.eq(true, "drugsSpec", drugsSpec);
        cq.eq(true, "producersName", producersName);
        return getOne(cq, false);
    }

    @Override
    public List<EntityMap> selectLikeByDrugNameAndSpec(String drugsName, String drugsSpec) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugs.class);
        cq.select(BusDrugs.class, "drugsId", "drugsName", "drugsSpec", "producersName");
        cq.select(SysCompany.class, "companyName");
        cq.like("drugsName", drugsName);
        cq.like(true, "drugsSpec", drugsSpec);
        cq.createJoin(SysCompany.class);
        cq.orderByAsc("drugs.companyId");
        return selectEntityMap(cq);
    }


    /**
     * 移除用户数据权限配置
     *
     * @param userId
     * @return
     */
    private void removeDrugPolicy(Long policyDrugId) {
        UpdateWrapper<BusDrugs> wrapper = new UpdateWrapper();
        wrapper.set(true, "policyDrugId", null);
        wrapper.eq(true, "policyDrugId", policyDrugId);
        update(wrapper);
    }

    @Override
    public void saveAuthDrugs(Long policyDrugId, String... drugIds) {
        ApiAssert.isNotEmpty("结算政策不能为空", policyDrugId);
        // 先清空,在添加
        removeDrugPolicy(policyDrugId);
        if (FlymeUtils.isNotEmpty(drugIds)) {
            UpdateWrapper<BusDrugs> wrapper = new UpdateWrapper();
            wrapper.set(true, "policyDrugId", policyDrugId);
            wrapper.in(true, "drugsId", drugIds);
            update(wrapper);
        }
    }

    @Override
    public BusDrugs selectByDrugNo(String drugNo, Long companyId) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugs.class);
        cq.eq(true, "drugNo", drugNo);
        cq.eq(true, "companyId", companyId);
        return getOne(cq, false);
    }


    private BusDrugs selectByDrugsName(String drugsName, String drugsSpec, Long companyId) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugs.class);
        cq.eq("drugsName", drugsName);
        cq.eq("companyId", companyId);
        cq.eq(true, "drugsSpec", drugsSpec);
        return getOne(cq, false);
    }

    @Override
    public List<Long> getAuthList(Long policyDrugId) {
        CriteriaQuery cq = new CriteriaQuery(BusDrugs.class);
        cq.select("drugsId");
        cq.eq(true, "policyDrugId", policyDrugId);
        return listObjs(cq, e -> Long.parseLong(e.toString()));
    }


    @Override
    public List<EntityMap> selectLotNoByDrugsId(String drugNo, String hyDeptNo) {
        ApiAssert.isNotEmpty("参数不正确", drugNo);
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        EntityMap hyDept = hyRemoteOrderService.selectHyDeptByDeptNo(dbName, hyDeptNo);
        String storeNames = "";
        List<String> codes = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(hyDept)) {
            String manageStore = hyDept.get("manageStore");
            if (FlymeUtils.isNotEmpty(manageStore)) {
                storeNames = manageStore.replace("(", "").replace(")", "").replaceAll("|", "").replace("'", "");
                List<String> storeNameList = ConvertUtils.stringTolist(storeNames);
                List<EntityMap> storeCodeList = hyRemoteOrderService.selectStoreCode(dbName, storeNameList);
                for (EntityMap storeCode : storeCodeList) {
                    String code = storeCode.get("storeCode");
                    codes.add(code);
                }
            }
        }
        List<EntityMap> result = new ArrayList<>();
        //获取海鹰系统中的商品批号列表
        List<EntityMap> lotList = hyRemoteOrderService.selectLotNoByDrugNo(dbName, drugNo, codes);
        if (FlymeUtils.isNotEmpty(lotList)) {
            for (EntityMap entityMap : lotList) {
                //批号
                String drugLotNo = entityMap.get("drugLotNo");
                //仓库编码
                String storeCode = entityMap.get("storeCode");
                //库存编码
                Integer drugLotId = entityMap.getInt("drugLotId");
                //在单数
                BigDecimal onUseCount = entityMap.getBigDecimal("onUseCount");
                //本地未同步在单数
                //Integer localOnUserCount = orderDetailService.countCdCount(drugNo, drugLotNo, storeCode, 0);
                //onUseCount = onUseCount.add(new BigDecimal(localOnUserCount));
                //锁定数
                //BigDecimal lockCount = entityMap.getBigDecimal("lockCount");
                //entityMap.put("localOnUserCount", localOnUserCount);
                //现存数量
                BigDecimal surplusNum = entityMap.getBigDecimal("surplusNum");
                //surplusNum = surplusNum.subtract(lockCount);
                //可开票总数(现存数量-在单数-锁定数)
                BigDecimal canUseNum = surplusNum.subtract(onUseCount);
                //排除库存为0的
                if (FlymeUtils.gtzero(surplusNum)) {
                    //查询入库单细表同批号同仓库(如果有多条记录这说明同一批号存在多条记录)
                    //查询入库单细
                    List<EntityMap> drugLotList = hyRemoteOrderService.selectDrugLotList(dbName, drugNo, drugLotNo, storeCode);
                    if (FlymeUtils.isNotEmpty(drugLotList)) {
                        for (EntityMap map : drugLotList) {
                            BigDecimal costPrice = map.getBigDecimal("costPrice");
                            //入库数量
                            BigDecimal drugNum = map.getBigDecimal("drugNum");
                            //批号
                            String lotNo = map.get("drugLotNo");
                            //商品编码
                            String dn = map.get("drugNo");
                            //仓库编码
                            String sc = map.get("storeCode");
                            //已售数量
                            BigDecimal saleNum = hyRemoteOrderService.countCddx(dbName, dn, lotNo, sc, costPrice);
                            if (FlymeUtils.isEmpty(saleNum)) {
                                saleNum = new BigDecimal("0");
                            }
                            //其他出库数量
                            BigDecimal otherCount = hyRemoteOrderService.countOtherCount(dbName, dn, lotNo, sc, costPrice);
                            if (FlymeUtils.isEmpty(otherCount)) {
                                otherCount = new BigDecimal("0");
                            }
                            saleNum = saleNum.add(otherCount);
                            //本地未同步在单数
                            Integer localOnUserCount = orderDetailService.countCdCount(drugNo, drugLotNo, storeCode, 0, costPrice);

                            //待处理数
                            Integer lockCount= hyRemoteOrderService.selectLockCount(dbName,dn,drugLotNo,storeCode);

                            BigDecimal childCanUseNum = drugNum.subtract(saleNum);
                            BigDecimal childCanUseNum2 = childCanUseNum.subtract(new BigDecimal(localOnUserCount)).subtract(new BigDecimal(lockCount));
                            map.putAll(entityMap);
                            map.put("surplusNum", childCanUseNum);
                            map.put("canUseNum", childCanUseNum2);
                            map.put("inStoreNum", drugNum);
                            map.put("lockCount", lockCount);
                            map.put("saleNum", saleNum);
                            map.put("localOnUserCount", localOnUserCount);
                            map.put("costPrice", costPrice);
                            if (FlymeUtils.gtzero(childCanUseNum)) {
                                result.add(map);
                            }
                        }
                    }
                }
            }
        }
        //计算整件数和零货数
        if (FlymeUtils.isNotEmpty(result)) {
            for (EntityMap entityMap : result) {
                BigDecimal canUseNum = entityMap.getBigDecimal("canUseNum");
                Integer drugJr = entityMap.getInt("drugJr");
                if (FlymeUtils.isNotEmpty(drugJr) && drugJr > 0) {
                    BigDecimal wholeNum = canUseNum.divide(new BigDecimal(drugJr), 2, BigDecimal.ROUND_HALF_UP);
                    if (FlymeUtils.ge(wholeNum, new BigDecimal("1"))) {
                        Integer num = wholeNum.intValue();
                        entityMap.put("wholeNum", num);
                        entityMap.put("partNum", canUseNum.subtract(new BigDecimal(num).multiply(new BigDecimal(drugJr))));
                    }
                }
            }
        }
        return result;
    }


    private void initDrugLot(List<EntityMap> result, List<EntityMap> drugLotList, EntityMap entityMap, String dbName, String drugNo, String drugLotNo, String drugLotId, String storeCode, BigDecimal surplusNum, BigDecimal onUseCount, BigDecimal canUseNum) {
        //如果一个批号存在多个入库记录时需要按入库数量拆分显示
        if (FlymeUtils.isNotEmpty(drugLotList) && drugLotList.size() > 1) {
            //累计剩余总数(按入库日期倒序,最晚入库的就是剩余数量)
            BigDecimal totalDrugNum = new BigDecimal("0");
            List<EntityMap> list = new ArrayList<>();
            log.info("##################drugLotList{}", JSONUtil.toJsonStr(drugLotList));
            for (EntityMap map : drugLotList) {
                EntityMap newMap = new EntityMap();
                newMap.putAll(entityMap);
                //入库数量
                BigDecimal drugNum = map.getBigDecimal("drugNum");
                String orderNo = map.get("orderNo");
                Date drugDate = MapUtil.getDate(map, "drugDate");
                Date expirationDate = MapUtil.getDate(map, "expirationDate");
                //设置近效期
                setJxq(entityMap, expirationDate);
                //计算退货数量
                BigDecimal tuiCount = hyRemoteOrderService.countThCount1(dbName, orderNo, drugNo, drugLotNo, storeCode);
                log.info("##################入库数量{}", drugNum);
                log.info("##################计算退货数量{}", tuiCount);
                if (FlymeUtils.isNotEmpty(tuiCount)) {
                    drugNum = drugNum.add(tuiCount);
                }
                //具体入库时该批号的成本价
                BigDecimal costPrice = map.getBigDecimal("costPrice");
                totalDrugNum = totalDrugNum.add(drugNum);
                newMap.put("surplusNum", drugNum);
                newMap.put("costPrice", costPrice);
                newMap.put("drugLotId", drugLotId);
                newMap.put("drugDate", DateUtils.formatDate(drugDate, "yyyy-MM-dd"));

                //累计剩余数大于等于该批号的剩余数量(该节点说明需要截止到当前入库记录)
                if (FlymeUtils.ge(totalDrugNum, surplusNum)) {
                    //当前批号剩余量
                    BigDecimal surplusNum1 = drugNum.subtract(totalDrugNum.subtract(surplusNum));
                    newMap.put("surplusNum", surplusNum1);
                    list.add(newMap);
                    break;
                }
                list.add(newMap);
            }
            //按有效期正序
            Collections.reverse(list);
            //计算在单数
            if (FlymeUtils.isNotEmpty(list)) {
                for (EntityMap map : list) {
                    BigDecimal drugNum = map.getBigDecimal("surplusNum");
                    if (FlymeUtils.ge(drugNum, onUseCount) && FlymeUtils.gtzero(onUseCount)) {
                        map.put("onUseCount", onUseCount);
                        map.put("canUseNum", drugNum.subtract(onUseCount));
                    } else {
                        if (FlymeUtils.gtzero(onUseCount)) {
                            map.put("onUseCount", drugNum);
                            map.put("canUseNum", 0);
                        } else {
                            map.put("onUseCount", 0);
                            map.put("canUseNum", drugNum);
                        }
                    }
                    onUseCount = onUseCount.subtract(drugNum);
                }
                result.addAll(list);
            }
        }
        if (FlymeUtils.isNotEmpty(drugLotList) && drugLotList.size() == 1) {
            EntityMap map = drugLotList.get(0);
            String orderNo = map.get("orderNo");
            Date expirationDate = MapUtil.getDate(map, "expirationDate");
            //计算退货数量
            BigDecimal tuiCount = hyRemoteOrderService.countThCount(dbName, orderNo, drugNo, drugLotNo, storeCode);
            if (FlymeUtils.isNotEmpty(tuiCount)) {
                surplusNum = surplusNum.add(tuiCount);
            }
            setJxq(entityMap, expirationDate);
            entityMap.put("surplusNum", surplusNum);
            entityMap.put("canUseNum", canUseNum);
            result.add(entityMap);
        }
    }

    private void setJxq(EntityMap entityMap, Date expirationDate) {
        //近效期天数
        Integer dayNum = DateUtils.daysBetween(DateUtils.getNowDate(), expirationDate);
        entityMap.put("isJsq", 0);
        if (dayNum < 180) {
            entityMap.put("isJsq", 1);
        }
    }

    @Override
    public List<EntityMap> getHistoryDrug(String customCode, String drugNo) {
        ApiAssert.isNotEmpty("参数未传递", drugNo);
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        List<EntityMap> list = hyRemoteOrderService.getHistoryDrug(dbName, drugNo, customCode);
        return list;
    }

    @Override
    public List<EntityMap> selectTop10HyDrugList(String hyDeptNo, String title) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        EntityMap hyDept = hyRemoteOrderService.selectHyDeptByDeptNo(dbName, hyDeptNo);
        String storeNames = "";
        List<String> drugCodes = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(hyDept)) {
            String manageStore = hyDept.get("manageStore");
            if (FlymeUtils.isNotEmpty(manageStore)) {
                storeNames = manageStore.replace("(", "").replace(")", "").replaceAll("|", "").replace("'", "");
                List<String> storeNameList = ConvertUtils.stringTolist(storeNames);
                List<String> codes = new ArrayList<>();
                List<EntityMap> storeCodeList = hyRemoteOrderService.selectStoreCode(dbName, storeNameList);
                for (EntityMap storeCode : storeCodeList) {
                    String code = storeCode.get("storeCode");
                    codes.add(code);
                }
                List<EntityMap> drugCodeList = hyRemoteOrderService.selectDrugCodes(dbName, codes);
                if (FlymeUtils.isNotEmpty(drugCodeList)) {
                    for (EntityMap entityMap : drugCodeList) {
                        String drugNo = entityMap.get("drugNo");
                        //drugCodes.add(drugNo);
                    }
                }

            }
        }
        if (FlymeUtils.isNotEmpty(title)) {
            title = title.toUpperCase();
        }
        List<EntityMap> list = hyRemoteOrderService.selectTop10HyDrugListNew(dbName, title, drugCodes);
        return list;
    }


   /* @Override
    public List<EntityMap> selectTop10HyDrugList(String hyDeptNo, String title) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        EntityMap hyDept = hyRemoteOrderService.selectHyDeptByDeptNo(dbName, hyDeptNo);
        String storeNames = "";
        List<String> drugCodes = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(hyDept)) {
            String manageStore = hyDept.get("manageStore");
            if (FlymeUtils.isNotEmpty(manageStore)) {
                storeNames = manageStore.replace("(", "").replace(")", "").replaceAll("|", "").replace("'", "");
                List<String> storeNameList = ConvertUtils.stringTolist(storeNames);
                List<String> codes = new ArrayList<>();
                List<EntityMap> storeCodeList = hyRemoteOrderService.selectStoreCode(dbName, storeNameList);
                for (EntityMap storeCode : storeCodeList) {
                    String code = storeCode.get("storeCode");
                    codes.add(code);
                }
                List<EntityMap> drugCodeList = hyRemoteOrderService.selectDrugCodes(dbName, codes);
                if (FlymeUtils.isNotEmpty(drugCodeList)) {
                    for (EntityMap entityMap : drugCodeList) {
                        String drugNo = entityMap.get("drugNo");
                        drugCodes.add(drugNo);
                    }
                }

            }
        }
        if (FlymeUtils.isNotEmpty(title)) {
            title = title.toUpperCase();
        }
        List<EntityMap> list = hyRemoteOrderService.selectTop10HyDrugList(dbName, title, drugCodes);
        return list;
    }*/


    @Override
    @DSTransactional
    @DS("master")
    @Async("voucherExecutor")
    public void syncHyDrug(OpenUser openUser) {
        Long companyId = openUser.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        if (FlymeUtils.isNotEmpty(company)) {
            String dbName = company.getBusinessScope();
            ApiAssert.isNotEmpty("账套实例未配置", dbName);
            List<EntityMap> drugList = hyRemoteOrderService.selectHyDrugList(dbName);
            if (FlymeUtils.isNotEmpty(drugList)) {
                for (EntityMap entityMap : drugList) {
                    String drugNo = entityMap.get("drugNo");
                    String drugsName = entityMap.get("drugsName");
                    String producersName = entityMap.get("producersName");
                    String drugsSpec = entityMap.get("drugsSpec");
                    Integer drugJr = entityMap.getInt("drugJr");
                    String drugType = entityMap.get("drugType");
                    String supplierName = entityMap.get("supplierName");
                    String drugCode = entityMap.get("drugCode");
                    String drugUnit = entityMap.get("drugUnit");
                    BusDrugs busDrugs = selectByDrugNo(drugNo, companyId);
                    if (FlymeUtils.isEmpty(busDrugs)) {
                        busDrugs = new BusDrugs();
                        busDrugs.setDrugNo(drugNo);
                        busDrugs.setCompanyId(companyId);
                        busDrugs.setDrugNo(drugNo);
                    }
                    busDrugs.setDrugsName(drugsName);
                    busDrugs.setDrugsSpec(drugsSpec);
                    busDrugs.setDrugJr(drugJr);
                    busDrugs.setDrugsState(CommonConstants.INT_1);
                    busDrugs.setDrugsUnit(drugUnit);
                    busDrugs.setDrugsSpec(drugsSpec);
                    busDrugs.setDrugCode(drugCode);
                    busDrugs.setSupplierName(supplierName);
                    busDrugs.setDrugType(drugType);
                    busDrugs.setFloorPriceType(1);
                    if (DrugTypeEnum.ZS.getCode().equals(drugType)) {
                        busDrugs.setFloorPriceType(2);
                        busDrugs.setOwnerState(true);
                    }
                    busDrugs.setProducersName(producersName);
                    BusPolicyDrug policyDrug = policyDrugService.findPolicyDrugs(drugCode, drugsSpec, producersName);
                    if (FlymeUtils.isNotEmpty(policyDrug)) {
                        busDrugs.setPolicyDrugId(policyDrug.getPolicyDrugId());
                    }
                    saveOrUpdate(busDrugs);
                    //EntityMap params = new EntityMap();
                    //params.put("drugNo", drugNo);
                    //params.put("drugId", busDrugs.getDrugsId());
                    //hyRemoteOrderService.updateDrugId(dbName, params);
                }
            }
        }

    }

    @Override
    public ResultBody importData(MultipartFile file, Map params) {
        try {
            Long companyId = OpenHelper.getCompanyId();
            List<BusDrugs> drugList = importDate(companyId, file);
            if (drugList.size() > 0) {
                boolean b = updateBatchById(drugList);
                if (b) {
                    return ResultBody.msg("成功更新" + drugList.size() + "条记录");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.failed("更新失败");
    }

    /**
     * 读取零售价
     */
    private List<BusDrugs> importDate(Long companyId, MultipartFile file) throws Exception {
        ImportParams importParams = new ImportParams();
        List<Map<Object, Object>> excelList2 = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, importParams);
        List<BusDrugs> drugList = new ArrayList<>();
        for (Map<Object, Object> objectMap : excelList2) {
            BigDecimal retailPrice = TypeUtils.castToBigDecimal(objectMap.get("零售价"));
            String drugsName = TypeUtils.castToString(objectMap.get("商品名称"));
            String drugsSpec = TypeUtils.castToString(objectMap.get("规格"));
            if (FlymeUtils.gtzero(retailPrice)) {
                BusDrugs drugs = selectByDrugsName(drugsName, drugsSpec, companyId);
                if (FlymeUtils.isNotEmpty(drugs)) {
                    drugs.setRetailPrice(retailPrice);
                    drugList.add(drugs);
                }
            }
        }
        return drugList;
    }


    @Override
    public ResultBody updateOnUseCount(Integer drugLotId, String drugNo, String drugLotNo, String storeCode) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        //结算系统在单数
        Integer localCdCount = orderDetailService.countCdCount(drugNo, drugLotNo, storeCode, 0, null);
        //出订单数
        Integer cdCount = hyRemoteOrderService.countCdCount(dbName, drugNo, drugLotNo, storeCode);
        //出库单数
        Integer ckCount = hyRemoteOrderService.countCkCount(dbName, drugNo, drugLotNo, storeCode);
        Integer result = (localCdCount + cdCount) - ckCount;
        EntityMap entity = new EntityMap();
        entity.put("drugLotId", drugLotId);
        entity.put("dbName", dbName);
        entity.put("drugNo", drugNo);
        entity.put("saleNum", result);
        hyRemoteOrderService.updateOnUserCount(dbName, entity);
        return ResultBody.msg("计算成功");
    }

    @Override
    public ResultBody updateDrugOnUseCount(String drugNo) {
        Long companyId = OpenHelper.getCompanyId();
        SysCompany company = companyService.getById(companyId);
        ApiAssert.isNotEmpty("账套信息不存在", company);
        String dbName = company.getBusinessScope();
        ApiAssert.isNotEmpty("账套实例未配置", dbName);
        //结算系统在单数
        Integer localCdCount = orderDetailService.countCdCount(drugNo, null, null, 1, null);
        //出订单数
        Integer cdCount = hyRemoteOrderService.countCdCount(dbName, drugNo, null, null);
        //出库单数
        Integer ckCount = hyRemoteOrderService.countCkCount(dbName, drugNo, null, null);
        Integer result = (cdCount) - ckCount;
        EntityMap entity = new EntityMap();
        entity.put("dbName", dbName);
        entity.put("drugNo", drugNo);
        entity.put("saleNum", result);
        hyRemoteOrderService.updateDrugOnUserCount(dbName, entity);
        return ResultBody.msg("计算成功");
    }
}
